{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import missingno as msno\n",
    "import pandas as pd\n",
    "import os\n",
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "import matplotlib.dates as mdates\n",
    "import matplotlib as mpl\n",
    "mpl.rcParams['agg.path.chunksize'] = 10000\n",
    "plt.rcParams[\"font.family\"] = \"Times New Roman\"\n",
    "plt.rcParams[\"font.size\"] = 10"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "filepath = 'Electricity Consumption/'\n",
    "dirs = os.listdir(filepath)\n",
    "dirs.sort()\n",
    "dirs = dirs[1:]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "###missing data\n",
    "mfig = msno.matrix(data, labels=True,fontsize=28, color=(58/255,95/255,205/255),label_rotation=90, width_ratios=(13, 1),freq = 'AS-JAN',filter = 'top')# filter = 'bottom')\n",
    "msno.bar(data)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "###annual plot-18\n",
    "filter_set = ['U10','U99','U165','U256','U258','U263','U267','U271','U276','U280','U283','U317','U353','U357','U364','U380','U381','U386']\n",
    "filter_data = pd.read_csv('plot_figure/filter_data.csv')\n",
    "filter_data.index = pd.to_datetime(filter_data['Time'])\n",
    "num_set = ['a','b','c','d','e','f','g','h','i','j','k','l','m','m','o','p','q','r']\n",
    "fig = plt.figure(figsize=(25, 20), dpi=300)\n",
    "for i in range(18):\n",
    "    d = filter_data[filter_data['file'] ==filter_set[i]]['Value']\n",
    "    plt.subplot(6,3,i+1)\n",
    "    plt.plot(d,'b')\n",
    "    plt.title(num_set[i]+') '+filter_set[i])\n",
    "    plt.gca().xaxis.set_major_formatter(mdates.DateFormatter('%Y-%m'))\n",
    "    # plt.gca().xaxis.set_major_locator(mdates.MonthLocator(interval=6))\n",
    "    plt.xticks(rotation =45)\n",
    "fig.text(0.5, -0.01, 'Time (15 minutes interval)', ha='center')\n",
    "fig.text(-0.01, 0.5, 'Electricity consumption(kWh)', va='center', rotation='vertical')\n",
    "# plt.title('')\n",
    "fig.tight_layout(pad=0.4, w_pad=1.0, h_pad=2.0)\n",
    "fig.savefig('plot_figure/annual_18plot.svg', dpi=300,bbox_inches = 'tight')\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "##monthly\n",
    "filter_set2 = ['U10','U99','U165','U263','U283','U317','U364','U380','U381']\n",
    "num_set2 = ['a','b','c','d','e','f','g','h','i']\n",
    "fig = plt.figure(figsize=(25, 20), dpi=300)\n",
    "for i in range(9):\n",
    "    d = filter_data[filter_data['file'] ==filter_set2[i]].loc['2018-09-01 00:00:00':'2018-10-01 00:00:00']['Value']\n",
    "    plt.subplot(3,3,i+1)\n",
    "    plt.plot(d,'b')\n",
    "    plt.title(num_set2[i]+') '+filter_set2[i])\n",
    "    plt.gca().xaxis.set_major_formatter(mdates.DateFormatter('%d/%m'))\n",
    "    plt.gca().xaxis.set_major_locator(mdates.DayLocator(interval=7))\n",
    "    plt.xticks(rotation =45)\n",
    "    plt.grid(which = 'major',axis = 'x')\n",
    "\n",
    "fig.text(0.5, -0.02, 'Time (15 minutes interval)', ha='center',fontsize = 25)\n",
    "fig.text(-0.01, 0.5, 'Electricity consumption(kWh)', va='center', rotation='vertical',fontsize = 25)\n",
    "# plt.title('')\n",
    "fig.tight_layout(pad=0.4, w_pad=1.0, h_pad=2.0)\n",
    "fig.savefig('plot_figure/monthly_9plot.svg', dpi=300,bbox_inches = 'tight')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "####weekly\n",
    "fig = plt.figure(figsize=(25, 20), dpi=300)\n",
    "for i in range(9):\n",
    "    d = filter_data[filter_data['file'] ==filter_set2[i]].loc['2018-09-09 00:00:00':'2018-09-16 00:00:00']['Value']\n",
    "    plt.subplot(3,3,i+1)\n",
    "    plt.plot(d,'b')\n",
    "    plt.title(num_set2[i]+') '+filter_set2[i])\n",
    "    plt.gca().xaxis.set_major_formatter(mdates.DateFormatter('%a'))\n",
    "    plt.gca().xaxis.set_major_locator(mdates.WeekdayLocator(byweekday=[0,1,2,3,4,5,6]))\n",
    "    # plt.xticks(rotation =45)\n",
    "    plt.grid(which = 'major',axis = 'x')\n",
    "fig.text(0.5, -0.02, 'Time (15 minutes interval)', ha='center',fontsize = 25)\n",
    "fig.text(-0.01, 0.5, 'Electricity consumption(kWh)', va='center', rotation='vertical',fontsize = 25)\n",
    "# plt.title('')\n",
    "fig.tight_layout(pad=0.4, w_pad=1.0, h_pad=2.0)\n",
    "fig.savefig('plot_figure/weekly_9plot.svg', dpi=300,bbox_inches = 'tight')\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "####daily\n",
    "\n",
    "st = pd.to_datetime('00:00:00')\n",
    "end = pd.to_datetime('23:45:00')\n",
    "idt =  pd.date_range(st, end, freq='15T')\n",
    "fig = plt.figure(figsize=(25, 20), dpi=300)\n",
    "for i in range(9):\n",
    "    d1 = filter_data[filter_data['file'] ==filter_set2[i]].loc['2018-09-10 00:00:00':'2018-09-10 23:45:00']['Value']\n",
    "    d2 = filter_data[filter_data['file'] ==filter_set2[i]].loc['2018-09-09 00:00:00':'2018-09-09 23:45:00']['Value']\n",
    "    d1.index = idt\n",
    "    d2.index = idt\n",
    "    plt.subplot(3,3,i+1)\n",
    "    plt.plot(d1,'b',label = 'Monday')\n",
    "    plt.plot(d2,'r',label = 'Sunday')\n",
    "    plt.title(num_set2[i]+') '+filter_set2[i])\n",
    "    plt.gca().xaxis.set_major_formatter(mdates.DateFormatter('%H:%M'))\n",
    "    plt.gca().xaxis.set_major_locator(mdates.HourLocator(interval = 6))\n",
    "    # plt.xticks(rotation =45)\n",
    "    plt.grid(which = 'major',axis = 'x')\n",
    "    plt.legend()\n",
    "fig.text(0.5, -0.02, 'Hours (15 minutes interval)', ha='center',fontsize = 25)\n",
    "fig.text(-0.01, 0.5, 'Electricity consumption(kWh)', va='center', rotation='vertical',fontsize = 25)\n",
    "# plt.title('')\n",
    "fig.tight_layout(pad=0.4, w_pad=1.0, h_pad=2.0)\n",
    "fig.savefig('plot_figure/daily_9plot.svg', dpi=300,bbox_inches = 'tight')\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "###histogram\n",
    "import seaborn as sns\n",
    "# sns.set(font_scale=1.5)\n",
    "plt.rcParams[\"font.family\"] = \"Times New Roman\"\n",
    "plt.rcParams['figure.figsize']=10,10\n",
    "plt.rcParams[\"font.size\"] = 10\n",
    "edata = filter_data[filter_data['file'] =='U380'].loc['2018-01-01 00:00:00':'2018-12-31 23:45:00']['Value']\n",
    "wdata_file = pd.read_csv('process result/weather data process/weather_orig_masking_intp/W2.csv')\n",
    "wdata_file.index = pd.to_datetime(wdata_file['Time'])\n",
    "wdata = wdata_file.loc['2018-01-01 00:00:00':'2018-12-31 23:45:00'][['Temperature(F)','Humidity(%)']]\n",
    "ewdata = pd.concat([edata,wdata], axis = 1)\n",
    "ewdata = ewdata.rename(columns={'Value':'Electricity\\nconsumption(kWh)'})\n",
    "fig = sns.pairplot(ewdata, plot_kws=dict(marker=\".\", linewidth=1))# # \n",
    "fig.savefig('plot_figure/EC_weather.svg', bbox_inches = 'tight')#dpi=300,\n",
    "fig.savefig('plot_figure/EC_weather.png',dpi=300, bbox_inches = 'tight')#\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "###extreme weather\n",
    "st = pd.to_datetime('00:00:00')\n",
    "end = pd.to_datetime('23:45:00')\n",
    "idt =  pd.date_range(st, end, freq='15T')\n",
    "f = 'U380'\n",
    "filter_set = ['U10','U99','U165','U256','U258','U263','U267','U271','U276','U280','U283','U317','U353','U357','U364','U380','U381','U386']\n",
    "# filter_set =['U99','']\n",
    "for f in filter_set:\n",
    "    EW_set = ['Low temperature', 'High temperature','High humidity', 'High heat and humidity', 'Severe tropical storm', 'Stong typhoon', 'Various extreme weather']\n",
    "    # color_set = ['grey','deeppink','darkorange','crimson','g','aqua']\n",
    "    color_set = ['#F1D77E','#EF7A6D','#9DC3E7','#9394E7','#B1CE46','#63E398']\n",
    "    edata = filter_data[filter_data['file'] ==f].loc['2018-01-01 00:00:00':'2018-12-31 23:45:00']['Value']\n",
    "    ymin = int(min(edata)-3)\n",
    "    ymax = int(max(edata)+4)\n",
    "    fig = plt.figure(figsize=(20, 20), dpi=300)\n",
    "    plt.subplot(3,1,3)\n",
    "    plt.plot(edata,'b',label = 'EC')\n",
    "    plt.fill_between([pd.to_datetime('2018-02-01 00:04:00'),pd.to_datetime('2018-02-01 08:00:00')],ymin,ymax,facecolor = color_set[0], alpha = 0.7,label = 'Low temperature')\n",
    "    plt.fill_between([pd.to_datetime('2018-05-31 11:15:00'),pd.to_datetime('2018-05-31 16:15:00')],ymin,ymax,facecolor = color_set[1], alpha = 1.0,label = 'High temperature')\n",
    "    plt.fill_between([pd.to_datetime('2018-08-27 01:45:00'),pd.to_datetime('2018-08-27 23:45:00')],ymin,ymax,facecolor = color_set[2], alpha = 0.5,label = 'High humidty')\n",
    "    plt.fill_between([pd.to_datetime('2018-06-29 12:15:00'),pd.to_datetime('2018-06-29 16:30:00')],ymin,ymax,facecolor = color_set[3], alpha = 1.0,label = 'High heat and humidty')\n",
    "    plt.fill_between([pd.to_datetime('2018-11-01 08:00:00'),pd.to_datetime('2018-11-02 02:00:00')],ymin,ymax,facecolor = color_set[4], alpha = 0.5,label = 'Severe tropical storm')\n",
    "    plt.fill_between([pd.to_datetime('2018-09-16 04:00:00'),pd.to_datetime('2018-09-16 19:00:00')],ymin,ymax,facecolor = color_set[5], alpha = 0.8,label = 'Stong typhoon')\n",
    "    plt.ylim(ymin,ymax)\n",
    "\n",
    "    plt.title('g) Various extreme weather')\n",
    "    plt.gca().xaxis.set_major_formatter(mdates.DateFormatter('%Y-%m'))\n",
    "    # plt.gca().xaxis.set_major_locator(mdates.MonthLocator(interval = 6))# plt.xticks(rotation =45)\n",
    "    # plt.grid(which = 'major',axis = 'x')\n",
    "    plt.legend()\n",
    "\n",
    "    plt.subplot(3,3,1)\n",
    "    edata0 = edata.loc['2018-02-01 00:00:00':'2018-02-01 23:45:00']\n",
    "    edata1 = edata.loc['2018-01-31 00:00:00':'2018-01-31 23:45:00']\n",
    "    edata7 = edata.loc['2018-01-25 00:00:00':'2018-01-25 23:45:00']\n",
    "    edata0.index = idt\n",
    "    edata1.index = idt\n",
    "    edata7.index = idt\n",
    "    plt.plot(edata0,'b',label = 'D-0')\n",
    "    plt.plot(edata1,'g',label = 'D-1')\n",
    "    plt.plot(edata7,'r',label = 'D-7')\n",
    "    plt.fill_between([pd.to_datetime('00:04:00'),pd.to_datetime('08:00:00')],ymin,ymax,facecolor = color_set[0], alpha = 0.7)\n",
    "    plt.title(num_set2[0]+') '+EW_set[0])\n",
    "    plt.gca().xaxis.set_major_formatter(mdates.DateFormatter('%H:%M'))\n",
    "    plt.gca().xaxis.set_major_locator(mdates.HourLocator(interval = 6))    # plt.xticks(rotation =45)\n",
    "    # plt.grid(which = 'major',axis = 'x')\n",
    "    plt.ylim(ymin,ymax)\n",
    "    plt.legend()\n",
    "\n",
    "    plt.subplot(3,3,2)\n",
    "    edata0 = edata.loc['2018-05-31 00:00:00':'2018-05-31 23:45:00']\n",
    "    edata1 = edata.loc['2018-05-30 00:00:00':'2018-05-30 23:45:00']\n",
    "    edata7 = edata.loc['2018-05-24 00:00:00':'2018-05-24 23:45:00']\n",
    "    edata0.index = idt\n",
    "    edata1.index = idt\n",
    "    edata7.index = idt\n",
    "    plt.plot(edata0,'b',label = 'D-0')\n",
    "    plt.plot(edata1,'g',label = 'D-1')\n",
    "    plt.plot(edata7,'r',label = 'D-7')\n",
    "    plt.fill_between([pd.to_datetime('11:15:00'),pd.to_datetime('16:15:00')],ymin,ymax,facecolor = color_set[1], alpha = 0.5)\n",
    "    plt.title(num_set2[1]+') '+EW_set[1])\n",
    "    plt.gca().xaxis.set_major_formatter(mdates.DateFormatter('%H:%M'))\n",
    "    plt.gca().xaxis.set_major_locator(mdates.HourLocator(interval = 6))    # plt.xticks(rotation =45)\n",
    "    # plt.grid(which = 'major',axis = 'x')\n",
    "    plt.ylim(ymin,ymax)\n",
    "    plt.legend()\n",
    "\n",
    "    plt.subplot(3,3,3)\n",
    "    edata0 = edata.loc['2018-08-27 00:00:00':'2018-08-27 23:45:00']\n",
    "    edata1 = edata.loc['2018-08-26 00:00:00':'2018-08-26 23:45:00']\n",
    "    edata7 = edata.loc['2018-08-20 00:00:00':'2018-08-20 23:45:00']\n",
    "    edata0.index = idt\n",
    "    edata1.index = idt \n",
    "    edata7.index = idt\n",
    "    plt.plot(edata0,'b',label = 'D-0')\n",
    "    plt.plot(edata1,'g',label = 'D-1')\n",
    "    plt.plot(edata7,'r',label = 'D-7')\n",
    "    plt.fill_between([pd.to_datetime('01:45:00'),pd.to_datetime('23:45:00')],ymin,ymax,facecolor = color_set[2], alpha = 0.5)\n",
    "    plt.title(num_set2[2]+') '+EW_set[2])\n",
    "    plt.gca().xaxis.set_major_formatter(mdates.DateFormatter('%H:%M'))\n",
    "    plt.gca().xaxis.set_major_locator(mdates.HourLocator(interval = 6))    # plt.xticks(rotation =45)\n",
    "    # plt.grid(which = 'major',axis = 'x')\n",
    "    plt.ylim(ymin,ymax)\n",
    "    plt.legend()\n",
    "\n",
    "    plt.subplot(3,3,4)\n",
    "    edata0 = edata.loc['2018-06-29 00:00:00':'2018-06-29 23:45:00']\n",
    "    edata1 = edata.loc['2018-06-28 00:00:00':'2018-06-28 23:45:00']\n",
    "    edata7 = edata.loc['2018-06-22 00:00:00':'2018-06-22 23:45:00']\n",
    "    edata0.index = idt\n",
    "    edata1.index = idt \n",
    "    edata7.index = idt\n",
    "    plt.plot(edata0,'b',label = 'D-0')\n",
    "    plt.plot(edata1,'g',label = 'D-1')\n",
    "    plt.plot(edata7,'r',label = 'D-7')\n",
    "    plt.fill_between([pd.to_datetime('12:15:00'),pd.to_datetime('16:30:00')],ymin,ymax,facecolor = color_set[3], alpha = 0.5)\n",
    "    plt.title(num_set2[3]+') '+EW_set[3])\n",
    "    plt.gca().xaxis.set_major_formatter(mdates.DateFormatter('%H:%M'))\n",
    "    plt.gca().xaxis.set_major_locator(mdates.HourLocator(interval = 6))    # plt.xticks(rotation =45)\n",
    "    # plt.grid(which = 'major',axis = 'x')\n",
    "    plt.ylim(ymin,ymax)\n",
    "    plt.legend()\n",
    "\n",
    "    plt.subplot(3,3,5)\n",
    "    edata0 = edata.loc['2018-11-01 00:00:00':'2018-11-01 23:45:00']\n",
    "    edata1 = edata.loc['2018-10-31 00:00:00':'2018-10-31 23:45:00']\n",
    "    edata7 = edata.loc['2018-10-25 00:00:00':'2018-10-25 23:45:00']\n",
    "    edata0.index = idt\n",
    "    edata1.index = idt \n",
    "    edata7.index = idt\n",
    "    plt.plot(edata0,'b',label = 'D-0')\n",
    "    plt.plot(edata1,'g',label = 'D-1')\n",
    "    plt.plot(edata7,'r',label = 'D-7')\n",
    "    plt.fill_between([pd.to_datetime('08:00:00'),pd.to_datetime('23:45:00')],ymin,ymax,facecolor = color_set[4], alpha = 0.2)\n",
    "    plt.title(num_set2[4]+') '+EW_set[4])\n",
    "    plt.gca().xaxis.set_major_formatter(mdates.DateFormatter('%H:%M'))\n",
    "    plt.gca().xaxis.set_major_locator(mdates.HourLocator(interval = 6))    # plt.xticks(rotation =45)\n",
    "    # plt.grid(which = 'major',axis = 'x')\n",
    "    plt.ylim(ymin,ymax)\n",
    "    plt.legend()\n",
    "\n",
    "    plt.subplot(3,3,6)\n",
    "    edata0 = edata.loc['2018-09-16 00:00:00':'2018-09-16 23:45:00']\n",
    "    edata1 = edata.loc['2018-09-15 00:00:00':'2018-09-15 23:45:00']\n",
    "    edata7 = edata.loc['2018-09-09 00:00:00':'2018-09-09 23:45:00']\n",
    "    edata0.index = idt\n",
    "    edata1.index = idt \n",
    "    edata7.index = idt\n",
    "    plt.plot(edata0,'b',label = 'D-0')\n",
    "    plt.plot(edata1,'g',label = 'D-1')\n",
    "    plt.plot(edata7,'r',label = 'D-7')\n",
    "    plt.fill_between([pd.to_datetime('04:00:00'),pd.to_datetime('19:00:00')],ymin,ymax,facecolor = color_set[5], alpha = 0.5)\n",
    "    plt.title(num_set2[5]+') '+EW_set[5])\n",
    "    plt.gca().xaxis.set_major_formatter(mdates.DateFormatter('%H:%M'))\n",
    "    plt.gca().xaxis.set_major_locator(mdates.HourLocator(interval = 6))    # plt.xticks(rotation =45)\n",
    "    # plt.grid(which = 'major',axis = 'x')\n",
    "    plt.ylim(ymin,ymax)\n",
    "    plt.legend()\n",
    "\n",
    "    fig.text(0.5, 1.02, f, ha='center',fontsize = 25)\n",
    "    fig.text(0.5, -0.02, 'Time', ha='center',fontsize = 25)\n",
    "    fig.text(-0.02, 0.5, 'Electricity consumption(kWh)', va='center', rotation='vertical',fontsize = 25)\n",
    "    # plt.title('')\n",
    "    fig.tight_layout(pad=0.4, w_pad=1.0, h_pad=2.0)\n",
    "    fig.savefig('plot_figure/EW_impact_'+str(f)+'.svg',dpi=300, bbox_inches = 'tight')#\n",
    "\n",
    "\n",
    "\n",
    "\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
